﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    var isGameOver = false;
    var canvas, context;
    var gameStage;
    var preloadQ;
    var logoScreenImage, logoScreenBitmap;
    var newGame = true;
    var player;
    var scaleW = window.innerWidth / 1366;
    var scaleH = window.innerHeight / 768;
    var floorImage, floorBitmap;
    var playerIdleImage, playerIdleBitmap,ghostImage;
    var ghosts = [];
    var timeToAddNewGhost = 0;
    var ghostSpeed = 1.0;
    var scoreText;
     var playerScore = 0;

    app.onactivated = function (args) {
       
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll());
        }
    };

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };
    document.addEventListener("DOMContentLoaded", initialize, false);

    app.start();

    function initialize() {
        canvas = document.getElementById("gameCanvas");
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight
        context = canvas.getContext("2d");

        gameStage = new createjs.Stage(canvas);
        
        canvas.addEventListener("MSPointerUp", pointerUp, false);
       
          canvas.addEventListener("MSPointerMove", pointerMove, false);
         canvas.addEventListener("MSPointerDown", pointerDown, false);

        loadContent();
    }
 
    function pointerUp(event) {
        if (newGame) {
                newGame = false;
         }
        else {
            player.targetX = event.x;
                 player.targetY = event.y;
              }
    }

    function pointerDown(event) {
        if (newGame) {
               }
       else {
                player.targetX = event.x;
                    player.targetY = event.y;
                }
       }
    
     function pointerMove(event) {
            if (newGame) {
               }
             else {
                     player.targetX = event.x;
                     player.targetY = event.y;
                 }
         }

    function loadContent() {
        preloadQ = new createjs.LoadQueue();
        preloadQ.on("complete", prepareStage, this);

        preloadQ.loadManifest([
        { id: "logoScreen", src: "images/GFX/LogoScreen.png" },
        { id: "floor", src: "images/GFX/floor.png" },
        { id: "ghost", src: "images/GFX/Ghost.png" },
         { id: "playerIdle", src: "images/GFX/PlayerIdle.png" }
        ]);
        scoreText = new createjs.Text("Score: " + playerScore, "30px sans-serif", "yellow");
        scoreText.x = canvas.width / 2 - (scoreText.getMeasuredWidth() * scaleW / 2);
      scoreText.scaleX = scaleW;
         scoreText.scaleY = scaleH;
        scoreText.y = 30 * scaleH;
         scoreText.visible = true;
         gameStage.addChild(scoreText);
     
    }
   
    function prepareStage() {
        logoScreenImage = preloadQ.getResult("logoScreen");
        logoScreenBitmap = new createjs.Bitmap(logoScreenImage);
        logoScreenBitmap.scaleX = scaleW;
       logoScreenBitmap.scaleY = scaleH;
       gameStage.addChild(logoScreenBitmap);

       
    floorImage = preloadQ.getResult("floor");
       floorBitmap = new createjs.Bitmap(floorImage);
       floorBitmap.visible = false;
       floorBitmap.scaleX = scaleW;
       floorBitmap.scaleY = scaleH;
       gameStage.addChild(floorBitmap);
       
    playerIdleImage = preloadQ.getResult("playerIdle");
       playerIdleBitmap = new createjs.Bitmap(playerIdleImage);
       playerIdleBitmap.visible = false;
       player = new Player();

 
       ghostImage = preloadQ.getResult("ghost");

       gameStage.addChild(playerIdleBitmap);
       createjs.Ticker.setInterval(window.requestAnimationFrame);
       createjs.Ticker.addEventListener("tick", gameLoop);
    }
     function gameLoop() {
         update();
         draw();
     }
     function Ghost(gfx) {
             this.positionX = Math.random() * 5000 - 2500;
              this.positionY = Math.random() * 3000 - 1500;
          
            this.setStartPosition = function () {
                     if (this.positionX >= 0 && this.positionX <= window.innerWidth) {
                             this.positionX = -500;
                         }
            
                      if (this.positionY >= 0 && this.positionY <= window.innerHeight) {
                              this.positionY = -500;
                          }
                  }
           
              this.targetX = 0;
              this.targetY = 0;
           
              this.move = function (tX, tY) {
                      this.targetX = tX;
                      this.targetY = tY;
               
                  if (this.targetX > this.positionX) {
                              this.positionX += ghostSpeed;
                          }
                      if (this.targetX < this.positionX) {
                             this.positionX -= ghostSpeed;
                          }
                      if (this.targetY > this.positionY) {
                              this.positionY += ghostSpeed;
                          }
                      if (this.targetY < this.positionY) {
                              this.positionY -= ghostSpeed;
                          }
                  };
           
              this.isCollision = function (playerX, playerY, playerW, playerH) {
                      var centerX = this.positionX + (this.ghostBitmap.image.width * scaleW / 2);
                      var centerY = this.positionY + (this.ghostBitmap.image.height * scaleH / 2);
               
                      if ((centerX >= playerX - playerW / 2) && (centerX < playerX + playerW / 2)) {
                              if ((centerY >= playerY - playerH / 2) && (centerY < playerY + playerH / 2)) {
                                      return true;
                                  }
                          }
            
                      return false;
                 }
           
              this.ghostBitmap = gfx;
         }

     function Player() {
        this.positionX = window.innerWidth / 2;
         this.positionY = window.innerHeight / 2;
        
         this.targetX = this.positionX;
           this.targetY = this.positionY;
        
       this.width = playerIdleBitmap.image.width * scaleW;
     this.height = playerIdleBitmap.image.height * scaleH;
          }
      
     function update() {
         if (newGame) {
             logoScreenBitmap.visible = true;
             floorBitmap.visible = false;
             playerIdleBitmap.visible = false;

                   
              }
          else {
             if (isGameOver) {
                     isGameOver = false;
                     ghosts.length = 0;
                     gameStage.clear();
                     gameStage.addChild(logoScreenBitmap);
                          gameStage.addChild(floorBitmap);
                        gameStage.addChild(playerIdleBitmap);
                        gameStage.addChild(scoreText);
                       
                         gameStage.update();
                      }
             logoScreenBitmap.visible = false;
             floorBitmap.visible = true;
             playerIdleBitmap.visible = true;
             scoreText.visible = true;
             if (player.targetX > player.positionX) {
                    player.positionX += 60;
                         }
                 if (player.targetX < player.positionX) {
                         player.positionX -= 60;
                       }
                    if (player.targetY > player.positionY) {
                            player.positionY += 60;
                     }
                     if (player.targetY < player.positionY) {
                           player.positionY -= 60;
                     }
                     playerIdleBitmap.x = player.positionX - (player.width / 2);
                     playerIdleBitmap.y = player.positionY - (player.height / 2);
                     playerScore += 1;
                      scoreText.text = ("Score: " + playerScore);
                     timeToAddNewGhost -= 1;
                      if (timeToAddNewGhost < 0) {
                             timeToAddNewGhost = 1000
                              ghosts.push(new Ghost(new createjs.Bitmap(ghostImage)));
                              ghosts[ghosts.length - 1].setStartPosition();
                             gameStage.addChild(ghosts[ghosts.length - 1].ghostBitmap);
                          }
                      for (var i = 0; i < ghosts.length; i++)
                           {
                              ghosts[i].ghostBitmap.x = ghosts[i].positionX;
                              ghosts[i].ghostBitmap.y = ghosts[i].positionY;
                               ghosts[i].ghostBitmap.visible = true;
                              ghosts[i].move(player.positionX, player.positionY);
                              isGameOver = ghosts[i].isCollision(player.positionX, player.positionY, player.width, player.height);
                              if (isGameOver) {
                                  playerScore = 0;

                                  break;
                              }
                           }

         }
   }
   
    function draw() {
        gameStage.update();
    }
})();
